package com.yingdong.db.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.sql.*;

import com.yingdong.db.domain.DBObjects;
import com.yingdong.db.domain.${classname?cap_first};
import com.yingdong.db.utils.DBParamWrapper;
public class ${classname?cap_first}Dao extends BaseDao<${classname?cap_first}>{
	private static ${classname?cap_first}Dao instance;

	public static ${classname?cap_first}Dao getInstance() {
		if (instance == null)
			instance = new ${classname?cap_first}Dao();
		return instance;
	}
	
    public List<${classname?cap_first}> selectByConditions(Object... params) throws SQLException {
		String sql = "SELECT * FROM `${tablename}` WHERE 1=1";
		DBParamWrapper wrapper = new DBParamWrapper();
		for (int i = 0; i < params.length / 2; i++) {
			String key = (String) params[2 * i];
			sql += " AND `" + key + "`=?";
			Object value = params[2 * i + 1];
			wrapper.add(Types.JAVA_OBJECT, value);

		}
		return queryList(sql, wrapper);
	}

	public List<${autoIncreamentType}> insertBatch(Collection<${classname?cap_first}> collections) throws SQLException{
		List<DBParamWrapper> list = new ArrayList<>();
		for (${classname?cap_first} value : collections) {
			list.add(domainToWrapper(value));
		}
		String sql = "INSERT INTO  `${tablename}` (<#list fields as field><#if !field.autoIncrease><#if !field_has_next>`${field.name}`<#else>`${field.name}`,</#if></#if></#list>)  VALUES(<#list fields as field><#if !field.autoIncrease><#if !field_has_next>?<#else>?,</#if></#if></#list>)";
		return excute${autoIncreamentType}InsertBatch(sql, list);
	
	}
	
	public int[] updateBatch(Collection<${classname?cap_first}> collections) throws SQLException{
		List<DBParamWrapper> list = new ArrayList<>();
		for (${classname?cap_first} value : collections) {
			DBParamWrapper wrapper = domainToWrapper(value);
			<#list keys as field>  
			wrapper.add(Types.${field.jdbcTpye}, value.get${field.name?cap_first}());
 			</#list>  
 			list.add(wrapper);
		}

		String sql = "UPDATE `${tablename}` SET <#list fields as field><#if !field.prim><#if !field_has_next>`${field.name}`=?<#else>`${field.name}`=?,</#if></#if></#list>   WHERE 1=1 <#list keys as key>  AND `${key.name}`=?</#list>";
		return excuteSqlBatch(sql, list);
	
	}
	
	public int[] deleteBatch(Collection<${classname?cap_first}> collections) throws SQLException{
		List<DBParamWrapper> list = new ArrayList<>();
		for (${classname?cap_first} value : collections) {
			DBParamWrapper wrapper = new DBParamWrapper();
			<#list keys as field>  
			wrapper.add(Types.${field.jdbcTpye}, value.get${field.name?cap_first}());
 			</#list>  
 			list.add(wrapper);
		}

		String sql = "DELETE FROM `${tablename}` WHERE 1=1 <#list keys as key>AND `${key.name}`=?</#list>";
		return excuteSqlBatch(sql, list);
	}
	
	public int delete(${classname?cap_first} value) throws SQLException{
		DBParamWrapper wrapper = new DBParamWrapper();
		<#list keys as field>  
		wrapper.add(Types.${field.jdbcTpye}, value.get${field.name?cap_first}());
 		</#list>  
		String sql = "DELETE FROM `${tablename}` WHERE 1=1 <#list keys as key>AND `${key.name}`=?</#list>";
		return excuteSql(sql, wrapper);
		
	}
	public void save(Collection<${classname?cap_first}> list) throws SQLException {
		List<${classname?cap_first}> insertList = new ArrayList<>();
		List<${classname?cap_first}> updateList = new ArrayList<>();
		List<${classname?cap_first}> delList = new ArrayList<>();
		for (${classname?cap_first} t : list) {
			switch (t.getDbStatus()) {
			case DBObjects.DBSTATUS_UPDATED:
				break;
			case DBObjects.DBSTATUS_INSERT:
				insertList.add(t);
				break;
			case DBObjects.DBSTATUS_UPDATE:
				updateList.add(t);
				break;
			case DBObjects.DBSTATUS_DELETE:
				delList.add(t);
				break;
			case DBObjects.DBSTATUS_DELETED:
				break;
			default:
				break;
			}
		}
		try {
			List<${autoIncreamentType}> result = insertBatch(insertList);
			for (int i = 0; i < insertList.size(); i++) {
				${classname?cap_first} value = insertList.get(i);
				<#list fields as field>
				<#if field.autoIncrease>
				value.set${field.name?cap_first}(result.get(i));
				</#if>
				</#list>
				value.setDbStatus(DBObjects.DBSTATUS_UPDATED);
			}
		} catch (SQLException e) {
			throw e;
		}
		try {
			updateBatch(updateList);
			for (int i = 0; i < updateList.size(); i++) {
				${classname?cap_first} value = updateList.get(i);
				value.setDbStatus(DBObjects.DBSTATUS_UPDATED);
			}
		} catch (SQLException e) {
			throw e;
		}
		try {
			deleteBatch(delList);
			for (int i = 0; i < delList.size(); i++) {
				${classname?cap_first} value = delList.get(i);
				value.setDbStatus(DBObjects.DBSTATUS_DELETED);
			}
		} catch (SQLException e) {
			throw e;
		}
	}
	@Override
	public MailInfo resultSetToObject(ResultSet resultSet) throws SQLException {
		${classname?cap_first} value = new ${classname?cap_first}();
		<#assign itr = 1>   
		<#list fields as field>  
		value.set${field.name?cap_first}(resultSet.get${field.type?replace('Integer', 'Int' ) }(${itr}));
		<#assign itr = itr+1>   
 		</#list>  

		return value;
	}

	@Override
	public DBParamWrapper domainToWrapper(${classname?cap_first} value) {
		DBParamWrapper wrapper = new DBParamWrapper();
		<#list fields as field>  
		<#if !field.autoIncrease>
		wrapper.add(Types.${field.jdbcTpye}, value.get${field.name?cap_first}());
		</#if>
 		</#list>  

		return wrapper;
	}

  
}